﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
public class CreateRoomModel : MonoBehaviour
{
    EginUser user = EginUser.Instance;
    public List<GameObject> ChooseStatus1;
    public List<GameObject> ChooseBtn1;
    public List<GameObject> ChooseStatus2;
    public List<GameObject> ChooseBtn2;
    public List<GameObject> ChooseStatus3;
    public List<GameObject> ChooseBtn3;
    public GameObject CreatRoomBtn;
    public GameObject CloseBtn;
    public GameObject ReplaceCreateBtn;
    public GameObject GoldPatternBtn;
    public GameObject RoomCardPatternBtn;
    public GameObject GoldPatternObj;
    public GameObject RoomCardPatternObj;

    private bool isCardRoom;
    private bool isAgentRoom;

    private void Awake()
    {
        for (int i = 0; i < ChooseBtn1.Count; i++)
        {
            UIEventListener.Get(ChooseBtn1[i]).onClick = ButtonClick;
        }

        for (int i = 0; i < ChooseBtn2.Count; i++)
        {
            UIEventListener.Get(ChooseBtn2[i]).onClick = ButtonClick;
        }
        for (int i = 0; i < ChooseBtn3.Count; i++)
        {
            UIEventListener.Get(ChooseBtn3[i]).onClick = ButtonClick;
        }

        UIEventListener.Get(CreatRoomBtn).onClick = ButtonClick;
        UIEventListener.Get(CloseBtn).onClick = ButtonClick;
        UIEventListener.Get(ReplaceCreateBtn).onClick = ButtonClick;
        UIEventListener.Get(GoldPatternBtn).onClick = ButtonClick;
        UIEventListener.Get(RoomCardPatternBtn).onClick = ButtonClick;
    }
    void ButtonClick(GameObject button)
    {
        if (RoomCardPatternObj.activeSelf == true)
        {
            if (button.name == "1")
            {
                ChooseStatus1[0].SetActive(true);
                ChooseStatus1[1].SetActive(false);
                ChooseStatus1[2].SetActive(false);

            }
            else if (button.name == "2")
            {
                ChooseStatus1[0].SetActive(false);
                ChooseStatus1[1].SetActive(true);
                ChooseStatus1[2].SetActive(false);

            }
            else if (button.name == "3")
            {
                ChooseStatus1[0].SetActive(false);
                ChooseStatus1[1].SetActive(false);
                ChooseStatus1[2].SetActive(true);
            }
        }
        else if (GoldPatternObj.activeSelf == true)
        {
            if (button.name == "1")
            {
                ChooseStatus2[0].SetActive(true);
                ChooseStatus2[1].SetActive(false);
                ChooseStatus2[2].SetActive(false);

            }
            else if (button.name == "2")
            {
                ChooseStatus2[0].SetActive(false);
                ChooseStatus2[1].SetActive(true);
                ChooseStatus2[2].SetActive(false);

            }
            else if (button.name == "3")
            {
                ChooseStatus2[0].SetActive(false);
                ChooseStatus2[1].SetActive(false);
                ChooseStatus2[2].SetActive(true);
            }
            if (button.name == "4")
            {
                ChooseStatus3[0].SetActive(true);
                ChooseStatus3[1].SetActive(false);
                ChooseStatus3[2].SetActive(false);

            }
            else if (button.name == "5")
            {
                ChooseStatus3[0].SetActive(false);
                ChooseStatus3[1].SetActive(true);
                ChooseStatus3[2].SetActive(false);

            }
            else if (button.name == "6")
            {
                ChooseStatus3[0].SetActive(false);
                ChooseStatus3[1].SetActive(false);
                ChooseStatus3[2].SetActive(true);
            }

        }

        if (button.name == "CreatRoomBtn")
        {
            SendCreatRoom(false);

        }
        else if (button.name == "ReplaceCreateRoomBtn")
        {
            SendCreatRoom(true);
        }
        else if (button.name == "CloseBtn")
        {
            Destroy(gameObject);
        }
        else if (button.name == "GoldPatternBtn")
        {
            GoldPatternObj.SetActive(true);
            RoomCardPatternObj.SetActive(false);
        }
        else if (button.name == "RoomCardPatternBtn")
        {
            GoldPatternObj.SetActive(false);
            RoomCardPatternObj.SetActive(true);
        }
    }
    void SendCreatRoom(bool isAgentRoom)
    {
        int TotalRound = 0;
        int BaseScore = 0;
        if (RoomCardPatternObj.activeSelf == true)
        {
            for (int i = 0; i < ChooseStatus1.Count; i++)
            {
                if (ChooseStatus1[i].activeSelf)
                {

                    Debug.Log("roomTypeId:" + i);
                    if (i == 0)
                    {
                        TotalRound = 4;
                    }
                    else if (i == 1)
                    {
                        TotalRound = 8;
                    }
                    else if (i == 2)
                    {
                        TotalRound = 16;
                    }
                }

            }

            isCardRoom = true;           
        }
        else if (GoldPatternObj.activeSelf == true)
        {
            for (int i = 0; i < ChooseStatus2.Count; i++)
            {
                if (ChooseStatus2[i].activeSelf)
                {
                    Debug.Log("roomTypeId:" + i);
                    if (i == 0)
                    {
                        BaseScore = 10000;
                    }
                    else if (i == 1)
                    {
                        BaseScore = 50000;
                    }
                    else if (i == 2)
                    {
                        BaseScore = 100000;
                    }
                }

            }
            for (int i = 0; i < ChooseStatus3.Count; i++)
            {
                if (ChooseStatus3[i].activeSelf)
                {

                    Debug.Log("roomTypeId:" + i);
                    if (i == 0)
                    {
                        TotalRound = 4;
                    }
                    else if (i == 1)
                    {
                        TotalRound = 8;
                    }
                    else if (i == 2)
                    {
                        TotalRound = 16;
                    }
                }

            }

            isCardRoom = false;                  
        }

        if(user.gameType == 1){
            //Debug.Log("isCardRoom = " + isCardRoom);
            //MahjongOpenRoomRequest MR = new Pb.MahjongOpenRoomRequest { TotalRound = TotalRound, IsCardRoom = isCardRoom, BaseScore = BaseScore, IsAgentRoom = isAgentRoom };
            //SocketManager socketManager = SocketManager.Instance;
            //socketManager.SendProtobuf1(MR, MsgId.MahjongOpenRoomRequest);
            //EginMahjong.Instance.baseScore = BaseScore;
            //EginMahjong.Instance.totalRound = TotalRound;
        }
        else if (user.gameType == 2)
        {
            Debug.Log("isCardRoom = " + isCardRoom);
            NnOpenRoomRequest MR = new Pb.NnOpenRoomRequest { TotalRound = TotalRound, IsCardRoom = isCardRoom, BaseScore = BaseScore, IsAgentRoom = isAgentRoom };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(MR, MsgId.NnOpenRoomRequest);
            EginDN.Instance.baseScore = BaseScore;
            EginDN.Instance.totalRound = TotalRound;
        }
        else if (user.gameType == 3)
        {
            Debug.Log("isCardRoom = " + isCardRoom);
            DdzOpenRoomRequest MR = new Pb.DdzOpenRoomRequest { TotalRound = TotalRound, IsCardRoom = isCardRoom, BaseScore = BaseScore, IsAgentRoom = isAgentRoom };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(MR, MsgId.DdzOpenRoomRequest);
            EginDDZ.Instance.baseScore = BaseScore;
            EginDDZ.Instance.totalRound = TotalRound;
        }
        else if (user.gameType == 4)
        {
            Debug.Log("isCardRoom = " + isCardRoom);
            RunOpenRoomRequest MR = new Pb.RunOpenRoomRequest { TotalRound = TotalRound,IsCardRoom = isCardRoom,BaseScore = BaseScore,IsAgentRoom = isAgentRoom };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(MR, MsgId.RunOpenRoomRequest);
            EginPDK.Instance.baseScore = BaseScore;
            EginPDK.Instance.totalRound = TotalRound;
        }
       
    }

}
